<?php
/*
 * @Description:
 * @Author: 橘子味的心
 * @Date: 2020-11-13 15:02:02
 * @LastEditTime: 2020-11-15 14:21:39
 * @LastEditors: 橘子味的心
 * @Reference:
 */

namespace x_mier\neditor\controller;

use x_mier\neditor\config\Config;

/**
 * 基础控制器
 */
class Base
{
    protected $config = [];
    # 资源类型
    protected $mimeType = [
        'xml' => 'application/xml,text/xml,application/x-xml',
        'json' => 'application/json,text/x-json,application/jsonrequest,text/json',
        'js' => 'text/javascript,application/javascript,application/x-javascript',
        'css' => 'text/css',
        'rss' => 'application/rss+xml',
        'yaml' => 'application/x-yaml,text/yaml',
        'atom' => 'application/atom+xml',
        'pdf' => 'application/pdf',
        'text' => 'text/plain',
        'png' => 'image/png',
        'jpg' => 'image/jpg,image/jpeg,image/pjpeg',
        'gif' => 'image/gif',
        'csv' => 'text/csv',
        'html' => 'text/html,application/xhtml+xml,*/*',
        'exe' => 'application/octet-stream',
    ];
    public function __construct()
    {
        $this->config = new Config();
        if (!array_key_exists(request()->ext(), $this->mimeType)) {
            if ($this->config->get('auth_control')) {
                if (!call_user_func($this->config->get('auth_control'))) {
                    throw new \think\exception\HttpResponseException($this->error('auth error'));
                }
            }
        }
    }
    # 解析资源
    public function assets()
    {

        $assets_path = __DIR__ . '/../../';
        $path = str_replace("neditor/assets/", "assets/neditor/", request()->pathinfo());
        $ext = request()->ext();
        if ($ext) {
            $type = "text/html";
            $content = file_get_contents($assets_path . $path);
            if (array_key_exists($ext, $this->mimeType)) {
                $type = $this->mimeType[$ext];
            }
            return response($content, 200, ['Content-Length' => strlen($content)])->contentType($type);
        }
    }
    /**
     * 路径拼接
     * 1.传入的多个参数用DIRECTORY_SEPARATOR连接
     * 2.将\\替换为/
     *
     * @param array ...$args
     * @return string
     */
    public static function pathJoin(...$args): string
    {
        $path = implode(DIRECTORY_SEPARATOR, $args);
        $path = str_replace('//', '/', $path);
        $path = str_replace('\\', '/', $path);
        return $path;
    }

    /**
     * 返回错误信息
     *
     * @param string $msg
     * @param integer $code
     * @return \think\Response
     */
    protected function error($msg = 'ERROR', $code = 0)
    {
        return json(['state' => $msg, 'code' => $code]);
    }

    /**
     * 返回响应信息
     *
     * @param array $data
     * @param string $msg
     * @param integer $code
     * @return \think\Response
     */
    protected function success(array $data = [], $msg = 'SUCCESS', $code = 1)
    {
        if (empty($data['state'])) {
            $data['state'] = $msg;
        }
        if (empty($data['code'])) {
            $data['code'] = $code;
        }
        return json($data);
    }
}
